package com.his.dictcode.controller;

import com.his.config.annotation.CurrentUser;
import com.his.dictcode.entity.DictCode;
import com.his.dictcode.service.DictCodeService;
import com.his.dictcode.vo.CacheCodeVo;
import com.his.dictcode.vo.DictCodeVo;
import com.his.dictcode.vo.DetailVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Author zhoufei
 * @Date 2020/2/4
 */

@RestController
@RequestMapping(value="/api/dict_code")
public class DictCodeController extends ResultBaseController{
    private  final org.slf4j.Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    @Resource
    private DictCodeService dictCodeService;

    @PostMapping(value="/insert.do")
    public ResultUtil insertDictCode(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictCodeVo dictCodeVo){
        try {
            dictCodeService.insert(dictCodeVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/delete.do")
    public ResultUtil deleteDictCodeByKey(@CurrentUser DictEmployee dictEmployee,@RequestBody DictCodeVo dictCodeVo){
        try {
            dictCodeService.deleteByKey(dictCodeVo);
            return this.success("成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @RequestMapping("/query_all.do")
    public List<DictCode> query(@CurrentUser DictEmployee dictEmployee,DictCodeVo dictCodeVo){
        try {
            List<DictCode> resultlist = dictCodeService.queryAll(dictCodeVo);
            return resultlist;
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public ResultUtil updateById(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictCodeVo dictCodeVo){
        try {
            dictCodeService.updateById(dictCodeVo);
            return this.success("更新成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value = "/cache_code.do")
    public ResultUtil cacheCode(){
        try{
            dictCodeService.cacheCode();
            return this.success("缓存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/get_code.do")
    public ResultUtil getCacheCode(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated CacheCodeVo cacheCodeVo){
        try{
            return this.success("获取成功", dictCodeService.getCacheCode(cacheCodeVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value="/get_code_type_id.do")
    public ResultUtil getCodeByCodeTypeId(@CurrentUser DictEmployee dictEmployee,String codeTypeId){
        try{
            return this.success("获取成功", dictCodeService.getCodeByTypeId(dictEmployee,codeTypeId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 获取code详情
     * @param dictEmployee
     * @param detailVo
     * @return
     */
    @PostMapping(value="/get_code_detail.do")
    public ResultUtil getCodeDetail(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DetailVo detailVo){
        try{
            return this.success("获取成功", dictCodeService.getCodeDetail(detailVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 获取基础编码目录树
     * @param dictEmployee
     * @param codeTypeId
     * @return
     */
    @GetMapping(value="/get_code_tree.do")
    public ResultUtil getCodeTree(@CurrentUser DictEmployee dictEmployee,String codeTypeId){
        try{
            return this.success("获取成功！",dictCodeService.getCodeTree(codeTypeId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
